import mysql.connector

mydb = mysql.connector.connect(
    # host="39.103.180.180",  # 数据库主机地址
    # user="root",  # 数据库用户名
    # database="study",#数据库名称 没有的话插入操作报错
    # passwd="root"  # 数据库密码
    host="192.168.47.102",  # 数据库主机地址
    port=3306,
    user="ccmadmin",  # 数据库用户名
    database="my_blog",#数据库名称 没有的话插入操作报错
    passwd="211314wccA@"  # 数据库密码

)

print(mydb)
mycursor = mydb.cursor()
#mycursor.execute("SHOW DATABASES")
#mycursor.execute("CREATE TABLE sites (name VARCHAR(255), url VARCHAR(255))")
sql = "INSERT INTO sites (name, url) VALUES (%s, %s)"
val = ("RUNOOB", "https://www.runoob.com")
# val = [
#   ('Google', 'https://www.google.com'),
#   ('Github', 'https://www.github.com'),
#   ('Taobao', 'https://www.taobao.com'),
#   ('stackoverflow', 'https://www.stackoverflow.com/')
# ]
#插入单个
mycursor.execute(sql, val)
#插入多条
#mycursor.executemany(sql, val)

mydb.commit()
print("1 条记录已插入, ID:", mycursor.lastrowid)

print(mycursor)

for x in mycursor:
    print(x)